package edu.whu.demo.service;

import edu.whu.demo.dao.SupplierRepository;
import edu.whu.demo.entity.Supplier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SupplierService {
    @Autowired
    private SupplierRepository repository;

    public Supplier addSupplier(Supplier supplier){
        return repository.saveAndFlush(supplier);
    }

    public Supplier getSupplier(long id) {
        try{
            return repository.findById(id).get();
        }
        catch (Exception e){
            return null;
        }

    }

    public void updateSupplier(long id, Supplier supplier){
        repository.save(supplier);
    }

    public void deleteSupplier(long id) {
        repository.deleteById(id);
    }

    public void deleteRecordBySupplierId(long id) {
        repository.deleteBySupplierId(id);
    }
}
